﻿using Cms.Core.Application.IService.PermissionInfo;
using Cms.Core.Application.Service.PermissionInfo;
using Cms.Core.Application.Service.PermissionInfo.Dto;
using Cms.Core.AspNetMvc.Mvc;
using Cms.Core.Infrastructure;
using Cms.Core.Infrastructure.Common;
using Cms.Core.Infrastructure.Dependency;
using Cms.Core.Infrastructure.Ui;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Cms.Core.Infrastructure.Extension;
using Newtonsoft.Json.Linq;
using Cms.Core.Infrastructure.Helps;

namespace Cms.Core.Web.Controllers
{
    /// <summary>
    /// 权限信息接口
    /// </summary>
    [Display("权限信息")]
    public class PermissionController : WebApiController
    {

        private readonly IPermissionService _permissionService = null;

        public PermissionController(IPermissionService permissionService) => _permissionService = permissionService;



        /// <summary>
        /// 异步添加或者更新权限
        /// </summary>
        /// <param name="input">权限Dto</param>
        /// <returns></returns>
        [Display("异步添加或者更新权限"), HttpPost]
        public async Task<AjaxResult> AddOrUpdatePermissionAsync([FromBody]PermissionDto input)
        {

            var result = await _permissionService.UpdateOrAddPermissionAsync(input);
            return result.ToAjaxResult();
        }

        /// <summary>
        /// 异步得到权限分页数据
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        [Display("异步得到权限分页数据"), HttpPost]
        public async Task<PageData> GetPermissionPageDataAsync([FromBody]PageRequestData requestData)
        {

            var pageData = await _permissionService.GetPermissionPageDataAsync(requestData);
            return pageData.ToPageData();
        }


        /// <summary>
        /// 异步得到权限列表数据
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        [Display("异步得到权限列表数据"), HttpPost]
        public async Task<PageData<PermissionTreeItem>> GetPermissionListAsync([FromBody]PageRequestData requestData)
        {
      
            var pageData = await _permissionService.GetPermissionListAsync(requestData);
            return pageData.ToPageData();
        }

        /// <summary>
        /// 异步根据列表名称得到按钮
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>

        [Display("异步根据列表名称得到按钮"), HttpGet]
        public async Task<AjaxResult> GetButtonByParentNameAsync(string name)
        {
            var data= await _permissionService.GetCurrentUserAuthorizationAllButtonByParentNameAsync(name);
            return new AjaxResult("查询成功",data:data);
        }
        /// <summary>
        /// 得到权限类型下拉值
        /// </summary>
        /// <returns></returns>
        [Display("得到权限类型下拉值"), HttpGet]
        
        public AjaxResult GetPermissionTypeListItem()
        {
            List<SelectListItem> selectListItems = new List<SelectListItem>();
            foreach (var item in Enum.GetValues(typeof(PermissionType)))
            {
                MemberInfo memberInfo = item.GetType().GetMember(item.ToString())?.FirstOrDefault();

                var name = memberInfo.ToDescription();
                selectListItems.Add(new SelectListItem {

                    Text = name,
                    Value = ((byte)item),
                });

            }
            return new AjaxResult() {
                Success = true,
                Data= selectListItems,
                Message="查询成功",
                Type=AjaxResultType.Success,

            };
        }

        /// <summary>
        /// 异步得到资源树级联选择器
        /// </summary>
        /// <returns></returns>
        [Display("异步得到资源树级联选择器"), HttpGet]

        public async Task<AjaxResult> GetPermissionTreeCascaderItemAsync()
        {

            return (await _permissionService.GetPermissionTreeCascaderItemAsync()).ToAjaxResult();
        }

        /// <summary>
        /// 异步加载权限表单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Display("异步加载权限表单信息"), HttpPost]
        public async Task<AjaxResult> LoadPermissionFormAsync([FromBody]JObject id)
        {

            var newId = id.ToObject("id")?.AsTo<Guid>();
            return (await _permissionService.LoadPermissionFormAsync(newId)).ToAjaxResult(); ;
        }


        /// <summary>
        /// 异步软删除资源
        /// </summary>
        [Display("异步软删除资源"), HttpGet]

        public async Task<AjaxResult> SoftDeletePermissionAsync(Guid id)
        {

            return (await _permissionService.SoftDeletePermissionAsync(id)).ToAjaxResult();

        }

        /// <summary>
        /// 异步得到菜单
        /// </summary>
        [Display("异步得到菜单"), HttpPost]

        public async Task<AjaxResult> GetAllMenusAsync()
        {
            return (await _permissionService.GetAllMenusAsync()).ToAjaxResult();

        }

    }
}